package com.zmn.oms.third.zhimi;

import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.model.entity.dict.ChannelZhimiFault;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：智米常量类
 *
 * @author lhl
 * @date 2020/04/24 21:45
 */
@Component
public class ZhimiConsts {

    public static final int CHANNEL_ID = 33892;
    public static final String CHANNEL_NAME = "智米";

    public static boolean POWER_ON;
    public static String APP_ID;
    public static String APPKEY;
    public static String STORE_ID;   // 仓库编码
    public static String API_DOMAIN;    // 请求API接口域名

    public static final String PRODUCT_LINE_CODE_LV3 = "Z030101";   // 三级产品线编码

    /* 公用SN */
    public static final String SN_GLOBAL = "800000";

    /* 请求API接口地址 */
    public static final String PATH_GET_ORDER = "/api/yeyx/tpa/getlist";    // 拉取订单
    public static final String PATH_PUSH_STAUS = "/api/yeyx/tpa/addservicestage";    // 推送订单进度
    public static final String PATH_PUSH_COMPLETE_AC = "/api/yeyx/tpa/airfinish";  // 推送完成（空调）
    public static final String PATH_PUSH_COMPLETE = "/api/yeyx/tpa/notairfinish";  // 推送完成（非空调）
    public static final String PATH_GET_ORDER_CALLBACK = "/api/yeyx/tpa/getlistcallback";    // 拉取订单后的回调
    public static final String PATH_SN_CHECK = "/api/yeyx/tpa/sncheck";     // SN校验
    public static final String PATH_GET_FAULT = "/api/yeyx/tpa/getfaultList";   // 获取产品故障信息
    public static final String PATH_PART_APPLY = "/api/yeyx/tpa/partapply";  // 配件申请
    public static final String PATH_GET_FAULT_PART = "/api/yeyx/tpa/getproductfaultlist";    // 获取故障对应配件
    public static final String PATH_GET_PART_EXPRESS = "/api/yeyx/tpa/getmaterialapplication";     // 获取配件物流信息

    /* 订单服务类型 */
    public static final int SERVICE_TYPE_REPAIR = 1;    // 维修
    public static final int SERVICE_TYPE_RETUND = 2;    // 退机
    public static final int SERVICE_TYPE_EXCHANGE = 3;  // 换机
    public static final int SERVICE_TYPE_DETECTION = 4; // 检测
    public static final int SERVICE_TYPE_INSTALL = 7;   // 安装
    public static final int SERVICE_TYPE_SURVEY = 8;    // 勘测
    public static final int SERVICE_TYPE_CANCEL = 99;   // 取消订单

    /**
     * 获取智米服务类型的名称
     * @param serviceType
     * @return
     */
    public static final String getServiceTypeName(Integer serviceType) {
        if (serviceType == null) {
            return "null";
        }

        switch (serviceType) {
            case 1:
                return "维修";
            case 2:
                return "退机";
            case 3:
                return "换机";
            case 4:
                return "检测";
            case 7:
                return "安装";
            case 8:
                return "勘测";
            default:
                return String.format("未知类型：%d", serviceType);
        }
    }

    /**
     * 检查是否是合法的服务类型
     * @param serviceType
     * @return
     */
    public static boolean isServiceTypeUseable(Integer serviceType) {
        boolean useable = Objects.equals(SERVICE_TYPE_REPAIR, serviceType)
                || Objects.equals(SERVICE_TYPE_RETUND, serviceType)
                || Objects.equals(SERVICE_TYPE_EXCHANGE, serviceType)
                || Objects.equals(SERVICE_TYPE_DETECTION, serviceType)
                || Objects.equals(SERVICE_TYPE_INSTALL, serviceType)
                || Objects.equals(SERVICE_TYPE_SURVEY, serviceType);
        return useable;
    }

    /**
     * 通过啄木鸟产品ID获取对应的智米订单服务类型
     * @param productId
     * @return
     */
    public static Integer getServiceTypeByProductId(Integer productId) {
        if (productId == null) {
            return null;
        }

        switch (productId) {
            case 11364: // 维修智米智能马桶盖
                return SERVICE_TYPE_REPAIR;
            case 11365: // 退智米智能马桶盖
                return SERVICE_TYPE_RETUND;
            case 11366: // 换智米智能马桶盖
                return SERVICE_TYPE_EXCHANGE;
            case 11367: // 检测智米智能马桶盖
                return SERVICE_TYPE_DETECTION;
            case 11368: // 安装智米智能马桶盖
                return SERVICE_TYPE_INSTALL;
            case 11369: // 勘测智米智能马桶盖
                return SERVICE_TYPE_SURVEY;
            default:
                return null;
        }
    }

    /* 保内与保外常量 */
    public static final int WARRANTY_IN = 1;
    public static final int WARRANTY_OUT = 2;

    /* 检测类型 */
    public static final int DETECTION_DAP = 4;
    public static final int DETECTION_DOA = 5;

    /* 换机类型 */
    public static final int EXCHANGE_DAP = 1;
    public static final int EXCHANGE_DOA = 2;
    public static final int EXCHANGE_OTHER = 3;

    /* 退货类型 */
    public static final int RETURN_FORCE = 1;   // 7天无理由退货
    public static final int RETURN_BROKEN = 2;  // 质量问题
    public static final int RETURN_POST_WRONG = 3;  // 发错货退货

    /* 用于同步完成的接口参数 */
    public static final int TRUE = 1;
    public static final int FALSE = 2;

    /* 真假编码 */
    public static final int YES = 1;
    public static final int NO = 0;

    /* 接口交互类型代码，用于记录接口交互日志 */
    public static final int SYNC_CODE_CALLBACK = 1; // 录单回调通知
    public static final int SYNC_CODE_ACCEPT = 2; // 同步接单
    public static final int SYNC_CODE_DUTYTIME = 3; // 同步预约
    public static final int SYNC_CODE_CHANGE_DUTYTIME = 4; // 同步改约
    public static final int SYNC_CODE_DISTRIBUTE = 5; // 同步派单
    public static final int SYNC_CODE_VISITED = 6; // 同步上门
    public static final int SYNC_CODE_COMPLETE = 7; // 同步完成
    public static final int SYNC_CODE_CANCEL = 8; // 同步取消

    /**
     * 获取同步状态码的文字描述
     * @param code
     * @return
     */
    public static String getSyncCodeName(int code) {
        switch (code) {
            case SYNC_CODE_CALLBACK:
                return "录单回调通知";
            case SYNC_CODE_ACCEPT:
                return "同步接单";
            case SYNC_CODE_DUTYTIME:
                return "同步预约";
            case SYNC_CODE_CHANGE_DUTYTIME:
                return "同步改约";
            case SYNC_CODE_DISTRIBUTE:
                return "同步派单";
            case SYNC_CODE_VISITED:
                return "同步上门";
            case SYNC_CODE_COMPLETE:
                return "同步完成";
            case SYNC_CODE_CANCEL:
                return "同步取消";
            default:
                return "未知操作";
        }
    }

    /**
     * 通过三级机型获取物料编码
     * @param thirdModelCode
     * @return
     */
    public static MainPartCode getMainPartCode(String thirdModelCode) {
        if (StringUtil.isBlank(thirdModelCode)) {
            return null;
        }

        switch (thirdModelCode) {
            // 怡和：一年内以换代修，超过一年走维修；
            case "M300101" :
                return new MainPartCode("M300101", "E95TOS1901207", "E95TOS1901202S", "智米智能马桶盖-怡和", "怡和版", false);
            // 爱创：与 怡和版 共用一个机头配件；
            case "M300102" :
                return new MainPartCode("M300102", "E95TOS1901202", "E95TOS1901202S", "智米智能马桶盖-爱创", "爱创版", false);
            // 西马：
            case "M300103" :
                return new MainPartCode("M300103", "E5020121220002", "E5020121220002S", "智米智能马桶盖-西马", "西马版", false);
            // 马桶盖Pro：
            case "M300201" :
                return new MainPartCode("M300201", "E50301211D1002", "S50301211D1002", "智米智能马桶盖Pro", "Pro版", true);
            // 马桶盖S：
            case "M300301" :
                return new MainPartCode("M300301", "E50401211D1002", "S50401211D1002", "智米智能马桶盖S", "S版", true);
            // 暖风版：
            case "M300401" :
                return new MainPartCode("M300401", "E5050121121001", "S5050121121001", "智米智能马桶盖-暖风", "暖风版", true);
            // 洗烘版：
            case "M300501" :
                return new MainPartCode("M300501", "E5060121121001", "S5060121121001", "智米智能马桶盖-洗烘", "洗烘版", true);
            default:
                return null;
        }
    }

    /**
     * 通过三级机型获取物料编码
     * @param zmnServiceItemId
     * @return
     */
    public static NonePartsItem getNonePartsItem(Integer zmnServiceItemId) {
        if (zmnServiceItemId == null) {
            return null;
        }

        switch (zmnServiceItemId) {
            case 5572 :
                return new NonePartsItem(5572,"MM0084","调试");
            case 5573 :
                return new NonePartsItem(5573,"MM0085","清理异物");
            case 5574 :
                return new NonePartsItem(5574,"MM0086","二次装机");
            case 5575 :
                return new NonePartsItem(5575,"MM0087","教授用户使用方法");
            case 5576 :
                return new NonePartsItem(5576,"MM0088","用户拒绝修理");
            case 5577 :
                return new NonePartsItem(5577,"MM0089","查无故障");
            case 5594 :
                return new NonePartsItem(5594,"MM0092","VIP移整机");
            case 5595 :
                return new NonePartsItem(5595,"MM0093","VIP二次装机");
            case 5596 :
                return new NonePartsItem(5596,"MM0094","VIP拆整机");
            default:
                return null;
        }
    }

    /**
     * 将前端页面的原因编码转换成具体的勘测原因
     * @param code
     * @return
     */
    public static SurveyReason getSurveyReason(String code) {
        if (StringUtil.isBlank(code)) {
            return null;
        }

        switch (code) {
            case "NS0012" :
                return new SurveyReason("NS0012", "电源位置不满足");
            case "NS0013" :
                return new SurveyReason("NS0013", "角阀位置不满足");
            case "NS0009" :
                return new SurveyReason("NS0009", "马桶安装孔不适配");
            case "NS0008" :
                return new SurveyReason("NS0008", "马桶异形不适配");
            case "NS0011" :
                return new SurveyReason("NS0011", "水压环境不满足");
            case "NS0014" :
                return new SurveyReason("NS0014", "已上门，告知无需安装");
            case "NS0010" :
                return new SurveyReason("NS0010", "原马桶盖无法拆除");
            default:
                return null;
        }
    }

    /**
     * 通过编码获取快递公司
     * @param code
     * @return
     */
    public static ExpressCompany getExpressCompanyByCode(String code) {
        if (StringUtil.isBlank(code)) {
            return null;
        }

        switch (code) {
            case "1" :
                return new ExpressCompany("1", "shunfeng", "顺丰速运");
            case "2" :
                return new ExpressCompany("2", "kuayue", "跨越物流");
            case "3" :
                return new ExpressCompany("3", "jd", "京东快递");
            case "4" :
                return new ExpressCompany("4", "yuantong", "圆通速递");
            case "5" :
                return new ExpressCompany("5", "zhongtong", "中通快递");
            case "6" :
                return new ExpressCompany("6", "shentong", "申通快递");
            case "7" :
                return new ExpressCompany("7", "yunda", "韵达快递");
            case "8" :
                return new ExpressCompany("8", "youzhengbk", "中国邮政");
            case "9" :
                return new ExpressCompany("9", "debangwuliu", "德邦物流");
            case "10" :
                return new ExpressCompany("10", "baishiwuliu", "百世汇通");
            case "11" :
                return new ExpressCompany("11", "suning", "苏宁快递");
            case "12" :
                return new ExpressCompany("12", "tiantian", "天天快递");
            case "13" :
                return new ExpressCompany("13", "quanfengkuaidi", "全峰快递");
            case "14" :
                return new ExpressCompany("14", "youshuwuliu", "优速快递");
            case "15" :
                return new ExpressCompany("15", "guotongkuaidi", "国通快递");
            case "16" :
                return new ExpressCompany("16", "minghangkuaidi", "民航快递");
            case "17" :
                return new ExpressCompany("17", "jialidatong", "嘉里大通");
            case "18" :
                return new ExpressCompany("18", "zhaijisong", "宅急送");
            case "19" :
                return new ExpressCompany("19", "ems", "EMS快递");
            default:
                return null;
        }
    }

    /**
     * 取消缘由
     * @param zmnCancelCode
     * @return
     */
    public static CancelReason getCancelReasonByZmnCancelCode(Integer zmnCancelCode) {
        if (zmnCancelCode == null) {
            return new CancelReason(null, 19, "其它");
        }

        switch (zmnCancelCode.intValue()) {
            case 52:
                return new CancelReason(zmnCancelCode, 1, "未发现故障");
            case 53:
                return new CancelReason(zmnCancelCode, 2, "非智米品牌");
            case 31:
                return new CancelReason(zmnCancelCode, 3, "电话联系不上");
            case 54:
                return new CancelReason(zmnCancelCode, 4, "顾客无时间7天以上");
            case 55:
                return new CancelReason(zmnCancelCode, 5, "顾客拒修");
            case 43:
                return new CancelReason(zmnCancelCode, 6, "费用高不修");
            case 56:
                return new CancelReason(zmnCancelCode, 7, "配送型号和购买型号不符");
            case 22:
                return new CancelReason(zmnCancelCode, 8, "重复、错误登记");
            case 57:
                return new CancelReason(zmnCancelCode, 10, "顾客自己解决");
            case 58:
                return new CancelReason(zmnCancelCode, 11, "电话指导");
            case 59:
                return new CancelReason(zmnCancelCode, 12, "因顾客家中条件问题不能安装调试");
            case 60:
                return new CancelReason(zmnCancelCode, 19, "其它");
            default:
                return new CancelReason(zmnCancelCode, 19, "其它");
        }
    }

    /**
     * 获取非用料维修故障
     * @return
     */
    public static List<ChannelZhimiFault> getUnusePartFaults() {
        List<ChannelZhimiFault> faults = new ArrayList<>(18);
        faults.add(newChannelZhimiFault("URF", "RF类故障", "M30URF0703", "WIFI无法重置（非用料）"));
        faults.add(newChannelZhimiFault("URF", "RF类故障", "M30URF0704", "蓝牙异常（非用料）"));
        faults.add(newChannelZhimiFault("UQT", "其它", "M30UQT1007", "APP设置类异常（非用料）"));
        faults.add(newChannelZhimiFault("UQT", "其它", "M30UQT1009", "故障无复现（非用料）"));
        faults.add(newChannelZhimiFault("UQT", "其它", "M30UQT1010", "清洁（非用料）"));
        faults.add(newChannelZhimiFault("UQT", "其它", "M30UQT1011", "重装（非用料）"));
        faults.add(newChannelZhimiFault("UQT", "其它", "M30UQT1012", "调整（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1201", "划痕（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1202", "磕伤（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1204", "变色（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1207", "脏污破损（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1208", "电池鼓胀（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1210", "私修（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1211", "私拆（非用料）"));
        faults.add(newChannelZhimiFault("UWG", "外观", "M30UWG1212", "进液（非用料）"));
        faults.add(newChannelZhimiFault("UST", "七天无理由", "M30UST1301", "买错了（非用料）"));
        faults.add(newChannelZhimiFault("UST", "七天无理由", "M30UST1302", "七天无理由（非用料）"));
        faults.add(newChannelZhimiFault("UST", "七天无理由", "M30UST1303", "无法安装（非用料）"));
        return faults;
    }

    private static ChannelZhimiFault newChannelZhimiFault(String faultCodeLv2, String faultNameLv2,
                                                   String faultCodeLv3, String faultNameLv3) {
        ChannelZhimiFault fault = new ChannelZhimiFault();
        fault.setProductCode(ZhimiConsts.PRODUCT_LINE_CODE_LV3);
        fault.setProductName("分体式马桶盖");
        fault.setFaultCodeLv2(faultCodeLv2);
        fault.setFaultNameLv2(faultNameLv2);
        fault.setFaultCodeLv3(faultCodeLv3);
        fault.setFaultNameLv3(faultNameLv3);
        return fault;
    }

    @Value("${zhimi.power-on:false}")
    public void setPowerOn(boolean powerOn) {
        POWER_ON = powerOn;
    }
    @Value("${zhimi.appkey:null}")
    public void setAppkey(String appkey) {
        APPKEY = appkey;
    }
    @Value("${zhimi.appid:null}")
    public void setAppId(String appId) {
        APP_ID = appId;
    }
    @Value("${zhimi.storeid:null}")
    public void setStoreId(String storeId) {
        STORE_ID = storeId;
    }
    @Value("${zhimi.api-domain:null}")
    public void setApiDomain(String apiDomain) {
        API_DOMAIN = apiDomain;
    }

    /**
     * 私有构造器，防止外部实例化
     */
    private ZhimiConsts() {}

    /**
     * 勘测不满足的原因类
     */
    @Data
    public static class SurveyReason {
        /**
         * 原因编码
         */
        private String code;
        /**
         * 原因说明
         */
        private String name;

        public SurveyReason(String code, String name) {
            this.code = code;
            this.name = name;
        }
    }

    /**
     * 主物料配件编码
     */
    @Data
    public static class MainPartCode {
        /**
         * 是否仅支持以换代修
         */
        private boolean replaceOnly;
        /**
         * 三级机型编码
         */
        private String thirdModelCode;
        /**
         * 配件所属产品的产品名称
         */
        private String productName;
        /**
         * 产品简称
         */
        private String nickname;
        /**
         * 整机编码
         */
        private String mainCode;
        /**
         * 机头编码
         */
        private String headCode;

        public MainPartCode(String thirdModelCode, String mainCode, String headCode, String productName, String nickname, boolean replaceOnly) {
            this.thirdModelCode = thirdModelCode;
            this.mainCode = mainCode;
            this.headCode = headCode;
            this.productName = productName;
            this.nickname = nickname;
            this.replaceOnly = replaceOnly;
        }
    }

    /**
     * 快递公司
     */
    @Data
    public static class ExpressCompany {
        /**
         * 快递公司编号(智米接口用)
         */
        private String code;
        /**
         * 快递公司编号(快递公司接口用)
         */
        private String companyCode;
        /**
         * 快递公司名称
         */
        private String name;

        public ExpressCompany(String code, String companyCode, String name) {
            this.code = code;
            this.companyCode = companyCode;
            this.name = name;
        }
    }

    /**
     * 非用料维修配件与啄木鸟服务项对应关系
     */
    @Data
    public static class NonePartsItem {
        /**
         * 啄木鸟服务项ID
         */
        private Integer zmnServiceItemId;
        /**
         * 名称
         */
        private String name;
        /**
         * 编码
         */
        private String code;

        public NonePartsItem(Integer zmnServiceItemId, String code, String name) {
            this.zmnServiceItemId = zmnServiceItemId;
            this.code = code;
            this.name = name;
        }
    }

    /**
     * 取消缘由
     */
    @Data
    public static class CancelReason {
        /**
         * 啄木鸟取消缘由编码
         */
        private Integer zmnCode;
        /**
         * 智米取消缘由编码
         */
        private Integer code;
        /**
         * 智米取消缘由
         */
        private String name;

        public CancelReason(Integer zmnCode, Integer code, String name) {
            this.zmnCode = zmnCode;
            this.code = code;
            this.name = name;
        }
    }


}
